﻿Public Class DosCommandOutput
    Private m_CreateNoWindow As Boolean = False
    Private process As Process
    Public Property CreateNoWindow() As Boolean
        Get
            Return Me.m_CreateNoWindow
        End Get
        Set(ByVal value As Boolean)
            Me.m_CreateNoWindow = value
        End Set
    End Property
    ''' <summary>
    '''  执行DOS命令，返回DOS命令的输出
    ''' </summary>
    ''' <param name="path">DOS程序的路径</param>
    ''' <param name="dosCommand">dos程序</param>
    ''' <param name="outfile">输出文件</param>
    ''' <param name="strParameter">DOS程序的参数</param>
    ''' <param name="RedirectStandardOutput"></param>
    ''' <returns>返回输出，如果发生异常，返回空字符串</returns>
    ''' <remarks></remarks>
    Public Function Execute(ByVal path As String, ByVal dosCommand As String, ByVal outfile As String, Optional ByVal strParameter As String = "", Optional ByVal RedirectStandardOutput As Boolean = False) As String
        Return Execute(path, dosCommand, outfile, 0, strParameter, RedirectStandardOutput)
    End Function
    ''' <summary>
    ''' 执行DOS命令，返回DOS命令的输出
    ''' </summary>
    ''' <param name="dosCommand">dos命令</param>
    ''' <param name="milliseconds">等待命令执行的时间（单位：毫秒），如果设定为0，则无限等待</param>
    ''' <param name="strParameter">可用于输入文件</param>
    ''' <returns>返回输出，如果发生异常，返回空字符串</returns>
    Public Function Execute(ByVal path As String, ByVal dosCommand As String, ByVal outfile As String, ByVal milliseconds As Integer, Optional ByVal strParameter As String = "", Optional ByVal RedirectStandardOutput As Boolean = False) As String
        Dim output As String = "" '输出字符串
        If dosCommand <> Nothing And dosCommand <> "" Then
            process = New Process()     '创建进程对象
            Dim startInfo As ProcessStartInfo = New ProcessStartInfo()
            startInfo.WorkingDirectory = path '设置执行程序的路径
            startInfo.FileName = "cmd.exe" '设定需要执行的命令
            If strParameter <> "" Then
                startInfo.Arguments = "/C " + dosCommand & "<" & strParameter & "> " & outfile ' "> 1.txt" '设定参数，其中的“/C”表示执行完命令后马上退出
            Else
                startInfo.Arguments = "/C " + dosCommand & strParameter & "> " & outfile ' "> 1.txt" '设定参数，其中的“/C”表示执行完命令后马上退出
            End If
            startInfo.UseShellExecute = False '不使用系统外壳程序启动
            startInfo.RedirectStandardInput = False '不重定向输入
            startInfo.RedirectStandardOutput = RedirectStandardOutput '不重定向输出
            startInfo.CreateNoWindow = m_CreateNoWindow '创建窗口
            process.StartInfo = startInfo
            Try
                If process.Start() Then        '开始进程
                    If milliseconds = 0 Then
                        process.WaitForExit() '这里无限等待进程结束
                        output = process.StandardOutput.ReadToEnd() '读取进程的输出
                    Else
                        process.WaitForExit(milliseconds) '这里等待进程结束，等待时间为指定的毫秒
                        output = process.StandardOutput.ReadToEnd() '读取进程的输出
                    End If
                End If
            Catch

            End Try
            If process IsNot Nothing Then
                process.Close()
            End If
        End If
        Return output
    End Function

    ''' <summary>
    '''  执行DOS命令，返回DOS命令的输出
    ''' </summary>
    ''' <param name="path">DOS程序的路径</param>
    ''' <param name="dosCommand">dos程序</param>
    ''' <param name="outfile">输出文件</param>
    ''' <param name="strParameter">DOS程序的参数</param>
    ''' <param name="RedirectStandardOutput"></param>
    ''' <returns>返回输出，如果发生异常，返回空字符串</returns>
    ''' <remarks></remarks>
    Public Function ExecuteTif(ByVal path As String, ByVal dosCommand As String, ByVal outfile As String, Optional ByVal strParameter As String = "", Optional ByVal RedirectStandardOutput As Boolean = False) As String
        Return ExecuteDEM(path, dosCommand, outfile, 0, strParameter, RedirectStandardOutput)
    End Function
    ''' <summary>
    ''' 执行DOS命令，返回DOS命令的输出。DEM数据软件。
    ''' </summary>
    ''' <param name="dosCommand">dos命令</param>
    ''' <param name="milliseconds">等待命令执行的时间（单位：毫秒），如果设定为0，则无限等待</param>
    ''' <param name="strParameter">可用于输入文件</param>
    ''' <returns>返回输出，如果发生异常，返回空字符串</returns>
    Public Function ExecuteDEM(ByVal path As String, ByVal dosCommand As String, ByVal outfile As String, ByVal milliseconds As Integer, Optional ByVal strParameter As String = "", Optional ByVal RedirectStandardOutput As Boolean = False) As String
        Dim output As String = "" '输出字符串
        If dosCommand <> Nothing And dosCommand <> "" Then
            process = New Process()     '创建进程对象
            Dim startInfo As ProcessStartInfo = New ProcessStartInfo()
            startInfo.WorkingDirectory = path '设置执行程序的路径
            startInfo.FileName = "cmd.exe" '设定需要执行的命令
            If strParameter <> "" Then
                startInfo.Arguments = "/C " + dosCommand & strParameter & "> " & outfile ' "> 1.txt" '设定参数，其中的“/C”表示执行完命令后马上退出
            Else
                startInfo.Arguments = "/C " + dosCommand & strParameter & "> " & outfile ' "> 1.txt" '设定参数，其中的“/C”表示执行完命令后马上退出
            End If
            startInfo.UseShellExecute = False '不使用系统外壳程序启动
            startInfo.RedirectStandardInput = False '不重定向输入
            startInfo.RedirectStandardOutput = RedirectStandardOutput '不重定向输出
            startInfo.CreateNoWindow = m_CreateNoWindow '创建窗口
            process.StartInfo = startInfo
            Try
                If process.Start() Then        '开始进程
                    If milliseconds = 0 Then
                        process.WaitForExit() '这里无限等待进程结束
                        output = process.StandardOutput.ReadToEnd() '读取进程的输出
                    Else
                        process.WaitForExit(milliseconds) '这里等待进程结束，等待时间为指定的毫秒
                        output = process.StandardOutput.ReadToEnd() '读取进程的输出
                    End If
                End If
            Catch

            End Try
            If process IsNot Nothing Then
                process.Close()
            End If
        End If
        Return output
    End Function
    Public Sub close()
        process.Close()
    End Sub
End Class
